package com.itheima.health.common;

import com.alibaba.druid.sql.visitor.functions.If;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Set;
@Component//  定义任务实现类
public class DeleteImg {

    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 2 * * ?")//每隔5秒 清楚文件
    public void Deleteimg(){
        //根据Redis中的set集合进行差值计算
        //找差集，然后删除差集中的图片
      Set<String> diff=redisTemplate.boundSetOps(RedisConst.UPLOAD_KEY)
              .diff(RedisConst.ADD_KEY);

      if(diff!=null) {
          for (String s : diff) {
              //删除七牛云
              qiniuUtils.deleteFileFromQiniu(s);
              //删除Redis
              redisTemplate.boundSetOps(RedisConst.UPLOAD_KEY).remove(s);
              System.out.println("自定义执行清理垃圾文件"+s);
          }
      }
    }

}
